/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao

import entity.Test;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author JAVIER
 */
public class TestsDAO implements DAO <Test> {
    
    private Integer ID = 0;     
    
    private static final String SQL_INSERT = " INSERT INTO Tests ( name, normalPrice, rushPrice, hasRush) VALUES ( ?, ?,?,?)";
    
    private static final String SQL_SELECT = "SELECT * FROM Tests where id= ?";
    
    private static final String SQL_SELECT_PROPERTY = " SELECT * FROM Tests where ? = ?" ;
    
    private static final String SQL_SELECT_All = " SELECT * FROM Tests";
    
    private static final String SQL_UPDATE = "UPDATE Tests SET id = ?, name = ?, normalPrice = ?, rushPrice = ? hasRush= ? WHERE id = ? ";

    private static final String SQL_DELETE = " DELETE FROM Tests WHERE id = ?";
    
    Integer create(Test test, Connection connect) throws SQLException {

        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_INSERT, Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, test.getName());
            ps.setFloat(2, test.getNormalPrice());
            ps.setFloat(3,test.getRushPrice());
            ps.setBoolean(4,test.getHasRush());

            ID = ps.executeUpdate();
        } finally {
            close(ps);
        }
        return ID;
        //TODO implement
    }
    
    Boolean remove(Test test, Connection connect) throws SQLException {
        Boolean result = false;
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_DELETE);
            ps.setInt(1, test.getId());
            ps.executeUpdate();
            result = true;
        } finally {
            close(ps);
        }
        return result;
        //TODO implement
    }
    
    Boolean update(Test test, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_UPDATE, Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1, test.getId());
            ps.setString(2, test.getName());
            ps.setFloat(3, test.getNormalPrice());
            ps.setFloat(4,test.getRushPrice());
            ps.setBoolean(5,test.getHasRush());
            ps.setInt(5, test.getId());
            ID = ps.executeUpdate();
            
        } finally {
            close(ps);
        }
        return ID;
        //TODO implement
    }
    
    Test find(Integer id, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT);
            ps.setString(1, id);
            rs = ps.executeQuery();
            Test test = new Test();
            result = null;
            test = get_test(rs);
            return test;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List findAll(Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_All);
            rs = ps.executeQuery();
            List results = new ArrayList();
            Test test = new Test();
            result = null;
            while (rs.next()) {
                test = get_test(rs);
                results.add(test);}
            return result;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List select_by_property(String camp, String Value, Connection connect)throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_PROPERTY);
            ps.setString(1, camp);
            ps.setString(2, value);
            rs = ps.executeQuery();
            List results = new ArrayList();
            Test test = new Test();
            result = null;
            while (rs.next()) {
                test = get_test(rs);
                results.add(test);}
            return result;

        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
        
    Test get_test (ResultSet rs) throws SQLException {
 
        Test test = new Test();
        test.setId(rs.getInt("id"));
        test.setName(rs.getString("name"));
        test.setNormalPrice(rs.getFloat("normalPrice"));
        test.setRushPrice(rs.getString("rushPrice"));
        test.setHasRush(rs.getBoolean("hasRush"));
        
        
        return test;
    }
    
}


